#include <iostream>
#include <string>
#include <math.h>
using namespace std;

string reverseNum(int num,int radix){
	string revStr = "";
	while (num){
		revStr += (char)((num % radix)+(int)'0');
		num /= radix;
	}
	return revStr;
}

int myPow(int x, int y){
	if (y == 0){
		return 1;
	}
	else{
		return x * myPow(x, y - 1);
	}
}

int reverseNumInRadix(int num, int radix){
	string revStr = reverseNum(num,radix);
	int resNum = 0;
	int size = revStr.length() - 1;
	for (int i = 0; i <= size; ++i){
		if (revStr[i] != '0'){
			resNum += (revStr[i] - '0')*myPow(radix, size - i);
		}
	}
	return resNum;
}

bool isPrime(int num){
	if (num == 1){
		return false;
	}
	else if (num == 2){
		return true;
	}
	else if (num % 2 == 0){
		return false;
	}
	for (int i = 3; i <= sqrt(num) + 1; i+=2){
		if (num % i == 0){
			return false;
		}
	}
	return true;
}

int main(){
	int num = 0;
	int radix = 0;
	cin >> num >> radix;
	while (num >= 0){
		if (isPrime(num) && isPrime(reverseNumInRadix(num, radix))){
			cout << "Yes\n";
		}else{
			cout << "No\n";
		}
		cin >> num >> radix;
	}
	return 0;
}